package com.bambooshoot.controller.biz;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.bambooshoot.common.annotation.Log;
import com.bambooshoot.common.annotation.RepeatSubmit;
import com.bambooshoot.common.core.controller.BaseController;
import com.bambooshoot.common.core.domain.R;
import com.bambooshoot.common.core.page.PageQuery;
import com.bambooshoot.common.core.page.TableDataInfo;
import com.bambooshoot.common.enums.BusinessType;
import com.bambooshoot.common.utils.ExcelUtil;
import com.bambooshoot.common.validate.AddGroup;
import com.bambooshoot.common.validate.EditGroup;
import com.bambooshoot.domain.bo.BizIncomeDetailsBo;
import com.bambooshoot.domain.vo.BizIncomeDetailsVo;
import com.bambooshoot.service.BizIncomeDetailsService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收入明细
 *
 * @author mxc
 * @date 2024-07-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/biz/incomeDetails")
public class BizIncomeDetailsController extends BaseController {

    private final BizIncomeDetailsService bizIncomeDetailsService;

    /**
     * 查询收入明细列表
     */
    @SaCheckPermission("biz:incomeDetails:list")
    @GetMapping("/list")
    public TableDataInfo<BizIncomeDetailsVo> list(BizIncomeDetailsBo bo, PageQuery pageQuery) {
        return bizIncomeDetailsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出收入明细列表
     */
    @SaCheckPermission("biz:incomeDetails:export")
    @Log(title = "收入明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BizIncomeDetailsBo bo, HttpServletResponse response) {
        List<BizIncomeDetailsVo> list = bizIncomeDetailsService.queryList(bo);
        ExcelUtil.exportExcel(list, "收入明细", BizIncomeDetailsVo.class, response);
    }

    /**
     * 获取收入明细详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("biz:incomeDetails:query")
    @GetMapping("/{id}")
    public R<BizIncomeDetailsVo> getInfo(@NotNull(message = "主键不能为空")
                                         @PathVariable String id) {
        return R.ok(bizIncomeDetailsService.queryById(id));
    }

    /**
     * 新增收入明细
     */
    @SaCheckPermission("biz:incomeDetails:add")
    @Log(title = "收入明细", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BizIncomeDetailsBo bo) {
        return toAjax(bizIncomeDetailsService.insertByBo(bo));
    }

    /**
     * 修改收入明细
     */
    @SaCheckPermission("biz:incomeDetails:edit")
    @Log(title = "收入明细", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BizIncomeDetailsBo bo) {
        return toAjax(bizIncomeDetailsService.updateByBo(bo));
    }

    /**
     * 删除收入明细
     *
     * @param ids 主键串
     */
    @SaCheckPermission("biz:incomeDetails:remove")
    @Log(title = "收入明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(bizIncomeDetailsService.deleteWithValidByIds(List.of(ids), true));
    }
}
